package com.cskaoyan.service;

import com.cskaoyan.bean.bo.WxOrderListBO;
import com.cskaoyan.bean.data.BaseData;
import com.cskaoyan.bean.data.DashboardData;
import com.cskaoyan.bean.param.OrderParam;
import com.cskaoyan.bean.pojo.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.bean.wxbo.OrderCommentBo;
import com.cskaoyan.bean.wxbo.OrderCommitBo;
import com.cskaoyan.bean.wxvo.WxOrder;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
import org.apache.ibatis.javassist.expr.Cast;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ：dailin
 * @date ：
 * @description：
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;


    @Autowired
    OrderGoodsMapper orderGoodsMapper;

    @Autowired
    GoodsCommentMapper goodsCommentMapper;

    @Autowired
    CartMapper cartMapper;

    @Autowired
    MallCouponUserMapper mallCouponUserMapper;

    @Autowired
    MallCouponMapper mallCouponMapper;


    @Override
    public DearlingOrderListVo selectOrderListDl(OrderParam orderParam, User user) {
        //分页功能
        PageHelper.startPage(orderParam.getPage(), orderParam.getSize());

        OrderExample orderExample = new OrderExample();
        //排序信息
//        orderExample.setOrderByClause(orderParam.getPage() + "  " + orderParam.getSize());

        //构造查询条件
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());

        if (orderParam.getShowType() == 1) {
            criteria.andOrderStatusEqualTo((short) 101);
        } else if (orderParam.getShowType() == 2) {
            criteria.andOrderStatusEqualTo((short) 201);
        } else if (orderParam.getShowType() == 3) {
            criteria.andOrderStatusEqualTo((short) 301);
        } else if (orderParam.getShowType() == 4) {
            criteria.andOrderStatusEqualTo((short) 401);
        }

        //第一步，先查出order的数据来，填充到一部分到需要响应的类中
        List<Order> orderList = orderMapper.selectByExample(orderExample);

        //这里先查询到dearlingOrderListVo对象的数据总数count参数和分页总数totalPages
        DearlingOrderListVo dearlingOrderListVo = new DearlingOrderListVo();
        dearlingOrderListVo.setCount(orderList.size());
        dearlingOrderListVo.setTotalPages((int) Math.ceil(orderList.size() / 10));

        List<DataDTO> dataDTOList = new ArrayList<>();
        //这里是给最外面的对象里面的各个成员变量进行赋值
        for (Order order : orderList) {
            DataDTO data = new DataDTO();
            data.setActualPrice(order.getActualPrice());
            data.setId(order.getId());
            data.setIsGroupin(false);
            data.setOrderSn(order.getOrderSn());

            //判断订单状态
            if (order.getOrderStatus() == 101) {
                data.setOrderStatusText("未付款");
            } else if (order.getOrderStatus() == 102) {
                data.setOrderStatusText("用户取消");
            } else if (order.getOrderStatus() == 103) {
                data.setOrderStatusText("系统取消");
            } else if (order.getOrderStatus() == 201) {
                data.setOrderStatusText("已付款");
            } else if (order.getOrderStatus() == 202) {
                data.setOrderStatusText("申请退款");
            } else if (order.getOrderStatus() == 203) {
                data.setOrderStatusText("已退款");
            } else if (order.getOrderStatus() == 301) {
                data.setOrderStatusText("已发货");
            } else if (order.getOrderStatus() == 401) {
                data.setOrderStatusText("用户收货");
            } else {
                data.setOrderStatusText("系统收货");
            }

            //开始查询orderGoods表，获取goodsList和handleOption的值
            OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
            OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
            //根据订单表查询ordergoods表中的商品数据
            orderGoodsExampleCriteria.andOrderIdEqualTo(order.getId());
            List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);


            List<GoodsListDTO> goodsList = new ArrayList<>();

            //创建DataDTO对象里面的HandleOptionDTO()对象并给他赋值
            HandleOptionDTO handleOption = new HandleOptionDTO();
//            handleOption.setCancel(true);
//            handleOption.setComment(order.getComments());
//            handleOption.setConfirm(false);
//            handleOption.setDelete(false);
//            handleOption.setPay(true);
//            handleOption.setRebuy(false);
//            handleOption.setRefund(false);


            if (order.getOrderStatus() == 101) {
                handleOption.setCancel(true);
                handleOption.setComment(order.getComments());
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(true);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            } else if (order.getOrderStatus() == 101 || order.getOrderStatus() == 102) {
                handleOption.setCancel(false);
                handleOption.setComment(order.getComments());
                handleOption.setConfirm(false);
                handleOption.setDelete(true);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            } else if (order.getOrderStatus() == 201) {
                handleOption.setCancel(false);
                handleOption.setComment(order.getComments());
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(true);
            } else if (order.getOrderStatus() == 301) {
                handleOption.setCancel(false);
                handleOption.setComment(order.getComments());
                handleOption.setConfirm(true);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            } else if (order.getOrderStatus() == 401 || order.getOrderStatus() == 402) {
                handleOption.setCancel(false);
                handleOption.setComment(order.getComments());
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(true);
                handleOption.setRefund(false);
            } else {
                handleOption.setCancel(false);
                handleOption.setComment(order.getComments());
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            }


            //设置data对象handleOption变量的值
            data.setHandleOption(handleOption);


            for (OrderGoods orderGood : orderGoods) {
                //创建goodslist来获取订单里面的商品详情。在for循环外面创建list对象
                GoodsListDTO goodsListDTO = new GoodsListDTO();
                goodsListDTO.setId(orderGood.getId());
                goodsListDTO.setGoodsName(orderGood.getGoodsName());
                short number = orderGood.getNumber();
                goodsListDTO.setNumber((int) number);
                goodsListDTO.setPicUrl(orderGood.getPicUrl());
                goodsList.add(goodsListDTO);
            }
            data.setGoodsList(goodsList);

            dataDTOList.add(data);
        }

        dearlingOrderListVo.setData(dataDTOList);

        //这里先暂时这样写，让它不报错，等后面再看
        return dearlingOrderListVo;
    }

    @Override
    public OrderDetailDearling selectOrderDetail(OrderParam orderParam) {
        //从orderGoods表中查询数据
        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        //构造查询条件
        OrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderParam.getOrderId());
        //根据构造的查询条件去数据库中查询orderGoods中的数据
        List<OrderGoods> orderGoodsDetail = orderGoodsMapper.selectByExample(orderGoodsExample);

        //创建返回的OrderDetailVo类对象，并且对象名必须为data
        OrderDetailDearling data = new OrderDetailDearling();
        //再创建OrdeDetailVo里面的两个内部类
        OrderDetailDearling.OrderGoodsDTO orderGoods = new OrderDetailDearling.OrderGoodsDTO();
        OrderDetailDearling.OrderInfoDTO orderInfo = new OrderDetailDearling.OrderInfoDTO();

        ArrayList<OrderDetailDearling.OrderGoodsDTO> orderGoodsDTOS = new ArrayList<>();
        for (OrderGoods goods : orderGoodsDetail) {
            //这个for循环里面是为添加orderGoods这个list数组里面的数据
            OrderDetailDearling.OrderGoodsDTO orderGoodsDTO = new OrderDetailDearling.OrderGoodsDTO();
            orderGoodsDTO.setAddTime(goods.getAddTime());
            orderGoodsDTO.setComment(goods.getComment());
            orderGoodsDTO.setDeleted(goods.getDeleted());
            orderGoodsDTO.setGoodsId(goods.getGoodsId());
            orderGoodsDTO.setGoodsName(goods.getGoodsName());
            orderGoodsDTO.setGoodsSn(goods.getGoodsSn());
            orderGoodsDTO.setId(goods.getId());
            short number = goods.getNumber();
            orderGoodsDTO.setNumber((int) number);
            orderGoodsDTO.setOrderId(goods.getOrderId());
            orderGoodsDTO.setPicUrl(goods.getPicUrl());
            orderGoodsDTO.setPrice(goods.getPrice());
            orderGoodsDTO.setProductId(goods.getProductId());


            //*****************************  这是重点，一层套一层   **********************************
            ArrayList<String> specifications = new ArrayList<>();
            for (OrderGoods orderGoods1 : orderGoodsDetail) {
                String specifications1 = orderGoods1.getSpecifications();
                specifications.add(specifications1);
            }
            orderGoodsDTO.setSpecifications(specifications);


            orderGoodsDTOS.add(orderGoodsDTO);
        }

        //从表order中查询数据
        OrderExample orderExample = new OrderExample();
        //创建构造条件
        OrderExample.Criteria criteria = orderExample.createCriteria();
        //根据订单单号去构造条件，使用订单单号查询订单里面的数据
        criteria.andIdEqualTo(orderParam.getOrderId());
        List<Order> orderList = orderMapper.selectByExample(orderExample);
        OrderDetailingExpressVo orderDetailingExpressVo = new OrderDetailingExpressVo();
        for (Order order : orderList) {
            //这里是给OrderDetailVo.OrderInfoDTO()的对象赋值
            orderInfo.setActualPrice(order.getActualPrice());
            orderInfo.setAddTime(order.getAddTime());
            orderInfo.setAddress(order.getAddress());
            orderInfo.setConsignee(order.getConsignee());
            orderInfo.setCouponPrice(order.getCouponPrice());
            orderInfo.setFreightPrice(order.getFreightPrice());
            orderInfo.setGoodsPrice(order.getGoodsPrice());
            orderInfo.setId(order.getId());
            orderInfo.setMobile(order.getMobile());
            orderInfo.setOrderSn(order.getOrderSn());
            //这里也是需要根据订单状态代码来判断订单状态的，需要写一些if条件语句来进行判断
            orderInfo.setOrderStatusText("已收货");

            /**
             * 添加快递
             */
            if (order.getShipChannel() != null && order.getShipSn() != null) {
                orderDetailingExpressVo.setShipperName(order.getShipChannel());
                orderDetailingExpressVo.setLogisticCode(order.getShipSn());
            }

            //再创建OrderInfoDTO里面的内部类
            OrderDetailDearling.OrderInfoDTO.HandleOptionDTO handleOption = new OrderDetailDearling.OrderInfoDTO.HandleOptionDTO();
//            这里一样，同上，先暂时这样子，不进行判断
            //根据订单状态判断handleOption里面的成员变量的值
            if (order.getOrderStatus() == 101) {
                handleOption.setCancel(true);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(true);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            } else if (order.getOrderStatus() == 101 || order.getOrderStatus() == 102) {
                handleOption.setCancel(false);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setDelete(true);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            } else if (order.getOrderStatus() == 201) {
                handleOption.setCancel(false);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(true);
            } else if (order.getOrderStatus() == 301) {
                handleOption.setCancel(false);
                handleOption.setComment(false);
                handleOption.setConfirm(true);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            } else if (order.getOrderStatus() == 401 || order.getOrderStatus() == 402) {
                handleOption.setCancel(false);
                handleOption.setComment(true);
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(true);
                handleOption.setRefund(false);
            } else {
                handleOption.setCancel(false);
                handleOption.setComment(false);
                handleOption.setConfirm(false);
                handleOption.setDelete(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
            }

            orderInfo.setHandleOption(handleOption);


        }

        data.setOrderInfo(orderInfo);
        data.setOrderGoods(orderGoodsDTOS);
        data.setExpressInfo(orderDetailingExpressVo);

        return data;
    }


    @Override
    public BaseData selectOrderListWh(OrderParam orderParam) {
        //分页功能
        PageHelper.startPage(orderParam.getPage(), orderParam.getSize());

        OrderExample orderExample = new OrderExample();
        //排序信息
//        orderExample.setOrderByClause(orderParam.getPage() + "  " + orderParam.getSize());

        //构造查询条件
        OrderExample.Criteria criteria = orderExample.createCriteria();
        if (orderParam.getShowType() == 1) {
            criteria.andOrderStatusEqualTo((short) 101);

        } else if (orderParam.getShowType() == 2) {
            criteria.andOrderStatusEqualTo((short) 201);

        } else if (orderParam.getShowType() == 3) {
            criteria.andOrderStatusEqualTo((short) 301);
        } else if (orderParam.getShowType() == 4) {
            criteria.andOrderStatusEqualTo((short) 401);
        }
        List<Order> orderList = orderMapper.selectByExample(orderExample);

        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        long total = orderPageInfo.getTotal();


        return BaseData.ok(orderList, total);
    }

    @Override
    public boolean refundMoney(@RequestBody Integer orderId) {

        System.out.println(orderId);
        Order order = new Order();
        order.setId(orderId);
        order.setOrderStatus((short) 202);
        Date date = new Date();
        order.setUpdateTime(date);
        int refund = orderMapper.updateByPrimaryKeySelective(order);

        if (refund == 1) return true;
        return false;

    }

    @Override
    public boolean confirmMoney(Integer orderId) {

        System.out.println(orderId);

        Order order = new Order();
        order.setId(orderId);
        order.setOrderStatus((short) 401);
        Date date = new Date();
        order.setUpdateTime(date);
        order.setConfirmTime(date);

        int refund = orderMapper.updateByPrimaryKeySelective(order);

        if (refund == 1) return true;
        return false;
    }

    @Override
    public OrderGoods selectOrderGoodsByOrderIdAndGoodsId(Integer orderId, Integer goodsId) {

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderId)
                .andGoodsIdEqualTo(goodsId)
                .andDeletedEqualTo(false);

        List<OrderGoods> orderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);

        return orderGoods.get(0);

    }

    //删除订单
    @Override
    public boolean deleteOrderById(Integer orderId) {

        Date date = new Date();
        Order order = new Order();
        order.setUpdateTime(date);
        order.setDeleted(true);
        order.setId(orderId);

        int i = orderMapper.updateByPrimaryKeySelective(order);
        if (i == 1) return true;
        return false;

    }

    @Override
    public boolean addComment(OrderCommentBo orderCommentBo, User user) {

        GoodsComment goodsComment = new GoodsComment();
        //商品评论，类型为0
        goodsComment.setType(0);
        goodsComment.setDeleted(false);
        goodsComment.setValueId(orderCommentBo.getOrderGoodsId());
        if (orderCommentBo.getPicUrls() == null || orderCommentBo.getPicUrls().length == 0) {
            goodsComment.setHasPicture(false);
            String[] s = {""};
            goodsComment.setPicUrls(s);
        }
        if (orderCommentBo.getPicUrls() != null && orderCommentBo.getPicUrls().length != 0) {
            goodsComment.setHasPicture(true);
        }
        if (goodsComment.getHasPicture()) {
            goodsComment.setPicUrls(orderCommentBo.getPicUrls());
        }

        Date date = new Date();
        goodsComment.setAddTime(date);
        goodsComment.setUpdateTime(date);
        goodsComment.setContent(orderCommentBo.getContent());
        goodsComment.setStar((short) orderCommentBo.getStar());
        goodsComment.setUserId(user.getId());

        int insert = goodsCommentMapper.insert(goodsComment);

//        Order order = new Order();
//        order.setEndTime(date);
//        order.setComments((short) 1);
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setComment(1);
        orderGoods.setId(orderCommentBo.getOrderGoodsId());
        orderGoods.setUpdateTime(date);
        orderGoodsMapper.updateByPrimaryKeySelective(orderGoods);

        OrderGoods orderGoods1 = orderGoodsMapper.selectByPrimaryKey(orderCommentBo.getOrderGoodsId());

        OrderGoodsExample orderGoodsExample = new OrderGoodsExample();
        OrderGoodsExample.Criteria criteria = orderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(orderGoods1.getOrderId());


        Order order = orderMapper.selectByPrimaryKey(orderGoods1.getOrderId());

        Short comments = order.getComments();
        int i1 = comments - 1;
        order.setComments((short) i1);

        int i = orderMapper.updateByPrimaryKeySelective(order);

        if (i == 1) return true;
        return false;

    }

    @Override
    public boolean cancelOrder(int orderId) {
        Date date = new Date();
        Order order = new Order();
        order.setUpdateTime(date);
        order.setOrderStatus((short) 102);
        order.setId(orderId);

        int i = orderMapper.updateByPrimaryKeySelective(order);
        if (i == 1) return true;
        return false;
    }

    @Override
    public boolean prepayOrder(int orderId) {
        Date date = new Date();
        Order order = new Order();
        order.setUpdateTime(date);
        order.setOrderStatus((short) 201);
        order.setId(orderId);
        order.setPayTime(date);

        int i = orderMapper.updateByPrimaryKeySelective(order);
        if (i == 1) return true;
        return false;
    }

    @Override
    public boolean submitOrder(OrderCommitBo orderCommitBo, User user) {

        //没有地址，不下单
        if (orderCommitBo.getAddressId() == 0) return false;

        //添加订单，并付款
        Order order = new Order();

        //根据addressId查出address表，并查询region获得准确地址
        OrderForAddOrderAddressVo addressVo = cartMapper.selectUserAddressForAddOrder(orderCommitBo.getAddressId());

        String address = addressVo.getProvinces() + " " + addressVo.getCity() + " " +
                addressVo.getArea() + " " + addressVo.getAddress();
        //封入地址，收件人，手机
        order.setAddress(address);
        order.setConsignee(addressVo.getName());
        order.setMobile(addressVo.getMobile());
        //

        //封入空message
        order.setMessage("");
        //封入message
        if (orderCommitBo.getMessage() != null && !"".equals(orderCommitBo.getMessage())) {
            System.out.println("添加message：");
            order.setMessage(orderCommitBo.getMessage());
        }

        //设置订单状态为未付款
        order.setOrderStatus((short) 101);

        //由时间获得orderSn码
        String time = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(System.currentTimeMillis());
        order.setOrderSn(time);
        //userId
        order.setUserId(user.getId());

        //查询cart
        List<Cart> carts = null;
        if (orderCommitBo.getCartId() > 0) {
            CartExample cartExample = new CartExample();
            CartExample.Criteria criteria = cartExample.createCriteria();
            criteria.andIdEqualTo(orderCommitBo.getCartId());

            carts = cartMapper.selectByExample(cartExample);
        } else {
            //查询所有checked的cart
            CartExample cartExample = new CartExample();
            CartExample.Criteria criteria = cartExample.createCriteria();
            criteria.andUserIdEqualTo(user.getId())
                    .andDeletedEqualTo(true)
                    .andCheckedEqualTo(true);
            carts = cartMapper.selectByExample(cartExample);
        }

        //
        BigDecimal goodsPrice = new BigDecimal(0);
        for (Cart cart : carts) {
            //算出商品总价
            BigDecimal price = cart.getPrice();
            int number = cart.getNumber();
            goodsPrice = goodsPrice.add(price.multiply(BigDecimal.valueOf(number)));
        }
        order.setGoodsPrice(goodsPrice);

        //设置邮费？？？？
        order.setFreightPrice(new BigDecimal(0));
        //设置完整费？？
        order.setIntegralPrice(new BigDecimal(0));
        //团购费
        order.setGrouponPrice(new BigDecimal(0));
        //设置优惠券费用
        order.setCouponPrice(new BigDecimal(0));
        //若使用了优惠券
        //查询优惠券
        if (orderCommitBo.getCouponId() > 0) {
            MallCouponExample mallCouponExample = new MallCouponExample();
            MallCouponExample.Criteria criteria1 = mallCouponExample.createCriteria();
            criteria1.andIdEqualTo(orderCommitBo.getCouponId());

            List<MallCoupon> mallCoupons = mallCouponMapper.selectByExample(mallCouponExample);
            MallCoupon mallCoupon = mallCoupons.get(0);
            order.setCouponPrice(mallCoupon.getDiscount());
        }
        BigDecimal orderPrice = new BigDecimal(0);
        orderPrice = orderPrice.add(order.getGoodsPrice()).add(order.getFreightPrice())
                .add(order.getIntegralPrice()).add(order.getGrouponPrice());
        BigDecimal actualPrice = new BigDecimal(0);
        actualPrice = actualPrice.add(orderPrice).subtract(order.getCouponPrice()).subtract(order.getGrouponPrice());
        //设置订单价格，实际价格
        order.setOrderPrice(orderPrice);
        order.setActualPrice(actualPrice);
        order.setComments((short) carts.size());
        //设置时间
        Date nowDate = new Date();
        order.setAddTime(nowDate);
        order.setUpdateTime(nowDate);
        order.setDeleted(false);
        //插入订单
        int insert = orderMapper.insert(order);

        if (insert != 1) {
            System.out.println("订单插入错误");

            return false;
        }


        //插入orderGoods
        for (Cart cart : carts) {

            OrderGoods orderGoods = new OrderGoods();

            orderGoods.setOrderId(order.getId());
            orderGoods.setGoodsId(cart.getGoodsId());
            orderGoods.setGoodsName(cart.getGoodsName());
            orderGoods.setGoodsSn(cart.getGoodsSn());
            orderGoods.setProductId(cart.getProductId());
            orderGoods.setNumber(cart.getNumber());
            orderGoods.setPrice(cart.getPrice());
            orderGoods.setSpecifications(cart.getSpecifications());
            orderGoods.setPicUrl(cart.getPicUrl());
            orderGoods.setComment(0);
            orderGoods.setAddTime(nowDate);
            orderGoods.setUpdateTime(nowDate);
            orderGoods.setDeleted(false);

            int insert1 = orderGoodsMapper.insert(orderGoods);

            if (insert1 != 1) {
                System.out.println("添加orderGoods错误");
                return false;
            }
        }

        //删除Cart
        //不为0时，按CartId删除
        if (orderCommitBo.getCartId() != 0) {
            Cart cart = new Cart();
            cart.setDeleted(false);
            cart.setId(orderCommitBo.getCartId());
            int i = cartMapper.updateByPrimaryKeySelective(cart);
            if (i == 0) {
                System.out.println("单个购物车删除错误");
                return false;
            }
        } else {
            //删除购物车中所有选中的商品
            CartExample cartExample = new CartExample();
            CartExample.Criteria criteria = cartExample.createCriteria();
            criteria.andUserIdEqualTo(user.getId())
                    //delete:1未删除0已删除
                    .andDeletedEqualTo(true)
                    //checked:1选中,0未选中
                    .andCheckedEqualTo(true);
            Cart cart = new Cart();
            cart.setDeleted(false);
            int i = cartMapper.updateByExampleSelective(cart, cartExample);

            if (i == 0) {
                System.out.println("购物车删除错误");
                return false;
            }
        }

        //若使用优惠券，couponId，修改对应的优惠券
        if (orderCommitBo.getCouponId() > 0) {
            MallCouponUserExample mallCouponUserExample = new MallCouponUserExample();
            MallCouponUserExample.Criteria criteria = mallCouponUserExample.createCriteria();
            criteria.andUserIdEqualTo(user.getId())
                    .andCouponIdEqualTo(orderCommitBo.getCouponId())
                    .andDeletedEqualTo(false);

            MallCouponUser mallCouponUser = new MallCouponUser();
            Date date = new Date();

            //修改状态为1已使用，修改使用时间,增加orderId
            mallCouponUser.setUsedTime(date)
                    .setStatus((short) 1)
                    .setOrderId(order.getId());

            int i = mallCouponUserMapper.updateByExampleSelective(mallCouponUser, mallCouponUserExample);


            if (i != 1) {
                System.out.println("修改优惠券出错");
                return true;
            }
        }


        return true;
    }
}